#include<iostream>
#include<vector>
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int sz=nums.size();
        int third=sz-1;
        int second=sz-2;
        int first=sz-3;
        if(sz==1)
        {
            return nums[0];
        }
        while(1)
        {
            if(nums[third]==nums[second]&&nums[second]==nums[first]&&nums[third]==nums[first])
            {
                nums.pop_back();
                nums.pop_back();
                nums.pop_back();
            }
            else if(nums[third]==nums[second])
            {
                return nums[first];
            }
            else if(nums[third]==nums[first])
            {
                return nums[second];
            }
            else//(nums[second]==nums[first])
            {
                return nums[third];
            }

            if(first-1==0)
            {
                return nums[first-1];
            }
            third-=3;
            second-=3;
            first-=3;
           
        }
    }
};
